# 1、导包
from scrapy import Request
from scrapy.spiders import Spider
from ..items import DcItem


class dcSpider(Spider):
    name = 'dc'

    # 2、获取初始请求
    def start_requests(self):
        url = 'https://www.runoob.com/w3cnote/common-english-terminology-in-programming.html'
        header = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24'
        }
        # 注解：方便翻页的一个小方法 发送请求
        yield Request(url, headers=header)

    # 3、得到响应
    def parse(self, response, **kwargs):
        # 4、数据类型保存为字典
        item = DcItem()
        # 5、获取数据
        lis = response.xpath('//table[@class = "reference"]/tbody/tr')
        for i in lis:
            try:
                word = i.xpath('td[1]/text()').extract()[0]
                if word == ' ':
                    continue
                chinese = i.xpath('td[2]/text()').extract()[0]
                item["word"] = word
                # 截取第一个字母
                item["firstWord"] = word[1:2]
                item["chinese"] = chinese
                item["del_flag"] = "0"
                # 6、返回item的值 继续执行此函数
                yield item
            except:
                pass
